package com.lu.manage.modular.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.manage.core.common.constant.SystemConstant;
import com.lu.manage.core.common.exception.SysLogExcepetion;
import com.lu.manage.core.tips.SuccessTip;
import com.lu.manage.core.utils.ToolUtil;
import com.lu.manage.modular.system.mapper.ExceptionLogMapper;
import com.lu.manage.modular.system.model.ExceptionLog;
import com.lu.manage.modular.system.service.ExceptionLogService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Project LuBoot
 * @Author: zhanglu
 * @Date: 2019-11-16 13:20:46
 * @Description: 博客异常日志 服务实现类
 */
@Service
public class ExceptionLogServiceImpl extends ServiceImpl<ExceptionLogMapper, ExceptionLog> implements ExceptionLogService {

    @Override
    public Object listPage(Map<String, Object> map) throws SysLogExcepetion {
        SuccessTip successTip = new SuccessTip();
        try {
            Page page = new Page(Integer.valueOf((String)map.get(SystemConstant.PAGE)), Integer.valueOf((String)map.get(SystemConstant.LIMIT)));
            QueryWrapper queryWrapper = new QueryWrapper();
            map.forEach((k, v) -> {
                if(!k.equals(SystemConstant.PAGE) && !k.equals(SystemConstant.LIMIT)){
                    if(k.equals(SystemConstant.SEARCH_WORDS)){
                        queryWrapper.like("name", v);
                    }else {
                        queryWrapper.eq(k, v);
                    }
                }
            });
            IPage<ExceptionLog> exceptionLogList = this.baseMapper.selectPage(page, queryWrapper);
            successTip.setCount(exceptionLogList.getTotal());
            successTip.setData(exceptionLogList.getRecords());
        } catch (Exception e) {
            throw new SysLogExcepetion(Thread.currentThread(), e);
        }
        return successTip;
    }

    @Override
    public Object list(Map<String, Object> map) throws SysLogExcepetion {
        List<ExceptionLog> exceptionLogList = new ArrayList<>();
        try {
            QueryWrapper<ExceptionLog> queryWrapper = new QueryWrapper<>();
            if(ToolUtil.isNotEmpty(map)){
                map.forEach((k, v) -> {
                    queryWrapper.eq(k, v);
                });
            }
            exceptionLogList = this.baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            throw new SysLogExcepetion(Thread.currentThread(), e);
        }
        return exceptionLogList;
    }

    @Override
    public Object addObj(ExceptionLog exceptionLog) throws SysLogExcepetion {
        try {
            return this.baseMapper.insert(exceptionLog);
        } catch (Exception e) {
            throw new SysLogExcepetion(Thread.currentThread(), e);
        }
    }

    @Override
    public Object updateObj(ExceptionLog exceptionLog) throws SysLogExcepetion {
        try {
            return this.baseMapper.updateById(exceptionLog);
        } catch (Exception e) {
            throw new SysLogExcepetion(Thread.currentThread(), e);
        }
    }

    @Override
    public Object deleteObj(String data) throws SysLogExcepetion {
        try {
            List<ExceptionLog> exceptionLogList = JSONArray.parseArray(data, ExceptionLog.class);
            List<String> ids = exceptionLogList.stream().map(ExceptionLog::getId).collect(Collectors.toList());
            return this.baseMapper.deleteBatchIds(ids);
        } catch (Exception e) {
            throw new SysLogExcepetion(Thread.currentThread(), e);
        }
    }

}
